#RequireAdmin
#NoTrayIcon
#OnAutoItStartRegister

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <Array.au3> ; Required for _ArrayDisplay only.

Func Decode($value)
	Local $iPID = Run('oa3tool.exe /decodeHwHash='&$value, "", @SW_HIDE, $STDOUT_CHILD)
	ProcessWaitClose($iPID)

	Local $sOutput = StdoutRead($iPID)

	; Use StringSplit to split the output of StdoutRead to an array. All carriage returns (@CRLF) are stripped and @CRLF (line feed) is used as the delimiter.
	Local $aArray = StringSplit(StringTrimRight(StringStripCR($sOutput), StringLen(@CRLF)), @CRLF)

	Local $aRes=StringRegExp($aArray[10], '(?i)<p n="ProductKeyID" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
    Local $KeyID=$aRes[0];

	Local $aRes=StringRegExp($aArray[33], '(?i)<p n="SmbiosSystemSerialNumber" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
    Local $SNID=$aRes[0];

   $aRes=StringRegExp($aArray[47], '(?i)<p n="PhysicalMedium" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
   Switch $aRes[0]
	  Case 'Native802_11'
		    Local $aRes=StringRegExp($aArray[48], '(?i)<p n="MacAddress" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
			Local $WLANID=StringReplace($aRes[0],':','');
	  Case '802_3'
		 Local $aRes=StringRegExp($aArray[48], '(?i)<p n="MacAddress" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
		 Local $LANID=StringReplace($aRes[0],':','');
	  Case 'Bluetooth'
		 Local $aRes=StringRegExp($aArray[48], '(?i)<p n="MacAddress" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
		 Local $BLUEID=StringReplace($aRes[0],':','');
   EndSwitch

   $aRes=StringRegExp($aArray[49], '(?i)<p n="PhysicalMedium" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
   Switch $aRes[0]
	  Case 'Native802_11'
		    Local $aRes=StringRegExp($aArray[50], '(?i)<p n="MacAddress" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
			Local $WLANID=StringReplace($aRes[0],':','');
	  Case '802_3'
		 Local $aRes=StringRegExp($aArray[50], '(?i)<p n="MacAddress" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
		 Local $LANID=StringReplace($aRes[0],':','');
	  Case 'Bluetooth'
		 Local $aRes=StringRegExp($aArray[50], '(?i)<p n="MacAddress" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
		 Local $BLUEID=StringReplace($aRes[0],':','');
   EndSwitch

   $aRes=StringRegExp($aArray[51], '(?i)<p n="PhysicalMedium" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
   Switch $aRes[0]
	  Case 'Native802_11'
		    Local $aRes=StringRegExp($aArray[52], '(?i)<p n="MacAddress" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
			Local $WLANID=StringReplace($aRes[0],':','');
	  Case '802_3'
		 Local $aRes=StringRegExp($aArray[52], '(?i)<p n="MacAddress" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
		 Local $LANID=StringReplace($aRes[0],':','');
	  Case 'Bluetooth'
		 Local $aRes=StringRegExp($aArray[52], '(?i)<p n="MacAddress" v="(.*?)" />', $STR_REGEXPARRAYGLOBALMATCH)
		 Local $BLUEID=StringReplace($aRes[0],':','');
   EndSwitch

   FileWriteLine("keyInfo.txt",$KeyID&","&$SNID&","&$LANID&","&$WLANID&","&$BLUEID)

   If @error Then MsgBox($MB_SYSTEMMODAL, "", "It appears there was an error trying to find all the files in the current script directory.")
EndFunc

Func ReadFile($file)
    Local $aArray = FileReadToArray($file)
    Local $iLineCount = @extended
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error) ; An error occurred reading the current script file.
    Else
        For $i = 0 To $iLineCount - 1 ; Loop through the array. UBound($aArray) can also be used.
            Decode($aArray[$i]) ; Display the contents of the array.
			ConsoleWrite("Processed:"&$i+1&"/"&$iLineCount&@CR)
        Next
    EndIf
 EndFunc

If $CmdLine[0] = 1 Then
   ReadFile($CmdLine[1])
Else
   MsgBox(48, 'Error', '参数错误')
EndIf
